<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:UML="//org.omg/UML/1.3">
	<xsl:template match="/">
		<UMLEditor>
			<Model>
				<!-- Package -->
				<xsl:apply-templates select="//Model[@xmi.id]"/>
				<!-- Héritage -->
				<xsl:apply-templates select="(//Class | //AssociationClass)/Namespace.ownedElement/Generalization[@xmi.id]" mode="Generalization"/>
				<!-- Les implémentations -->
				<xsl:apply-templates select="//Model/Namespace.ownedElement/Generalization[@xmi.id]" mode="Realization"/>
				<!-- Les associations -->
				<xsl:apply-templates select="//Association[@xmi.id]" mode="Association"/>
				<xsl:apply-templates select="//AssociationClass[@xmi.id]" mode="Association"/>
			</Model>
		</UMLEditor>
	</xsl:template>
	<!-- ***************************************************Model******************************************************************* -->
	<!-- Model (Package) -->
	<xsl:template match="Model">
		<xsl:element name="Package">
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"/>
			<!-- Les classes et les énumérations -->
			<xsl:for-each select="Namespace.ownedElement/Class[@xmi.id]">
				<xsl:variable name="idClass" select="@xmi.id"/>
				<xsl:variable name="isEnum" select="boolean(//Stereotype[@name='enumeration']/Stereotype.extendedElement/ModelElement[@xmi.idref=$idClass])"/>
				<xsl:choose>
					<!-- énumération -->
					<xsl:when test="$isEnum='true'">
						<xsl:apply-templates select="." mode="Enumeration"/>
					</xsl:when>
					<!-- classe -->
					<xsl:otherwise>
						<xsl:apply-templates select="." mode="Class"/>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:for-each>
			<!-- Les classes d'associations -->
			<xsl:apply-templates select="//AssociationClass[@xmi.id]" mode="AssociationClass"/>
			<!-- Les interfaces -->
			<xsl:apply-templates select="//Interface[@xmi.id]" mode="Interface"/>
		</xsl:element>
	</xsl:template>
	<!-- Enumérations -->
	<xsl:template match="Class" mode="Enumeration">
		<xsl:element name="Enumeration">
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="final"><xsl:value-of select="@isLeaf"/></xsl:attribute>
			<!-- Literal -->
			<xsl:apply-templates select="Classifier.feature/Attribute[@xmi.id]" mode="Literal"/>
		</xsl:element>
	</xsl:template>
	<!-- Literal -->
	<xsl:template match="Attribute" mode="Literal">
		<xsl:element name="Property">
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="type"/>
			<xsl:attribute name="visibility"/>
			<xsl:attribute name="static"/>
			<xsl:attribute name="defaultValue"/>
		</xsl:element>
	</xsl:template>
	<!-- Classes -->
	<xsl:template match="Class" mode="Class">
		<xsl:element name="Class">
			<xsl:variable name="id" select="@xmi.id"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="abstract"><xsl:value-of select="@isAbstract | GeneralizableElement.isAbstract/@xmi.value"/></xsl:attribute>
			<xsl:attribute name="final">false</xsl:attribute>
			<xsl:variable name="stereotype" select="//Stereotype/Stereotype.extendedElement/ModelElement[@xmi.idref=$id]"/>
			<xsl:attribute name="stereotype"><xsl:value-of select="$stereotype/../../@name"/></xsl:attribute>
			<xsl:attribute name="associationClass">false</xsl:attribute>
			<!-- Attributs -->
			<xsl:apply-templates select="Classifier.feature/Attribute[@xmi.id]" mode="Property"/>
			<!-- Opérations -->
			<!--<xsl:apply-templates select="Classifier.feature/Operation[@xmi.id]" mode="Operation"/>-->
		</xsl:element>
	</xsl:template>
	<!-- Classes d'associations -->
	<xsl:template match="AssociationClass" mode="AssociationClass">
		<xsl:element name="Class">
			<xsl:variable name="id" select="@xmi.id"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir--></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="abstract"><xsl:value-of select="@isAbstract | GeneralizableElement.isAbstract/@xmi.value"/></xsl:attribute>
			<xsl:attribute name="final">false</xsl:attribute>
			<xsl:variable name="stereotype" select="//Stereotype/Stereotype.extendedElement/ModelElement[@xmi.idref=$id]"/>
			<xsl:attribute name="stereotype"><xsl:value-of select="$stereotype/../../@name"/></xsl:attribute>
			<xsl:attribute name="associationClass">true</xsl:attribute>
			<!-- Attributs -->
			<xsl:apply-templates select="Classifier.feature/Attribute[@xmi.id]" mode="Property"/>
			<!-- Opérations -->
			<!--<xsl:apply-templates select="Classifier.feature/Operation[@xmi.id]" mode="Operation"/>-->
		</xsl:element>
	</xsl:template>
	<!-- Interfaces -->
	<xsl:template match="Interface" mode="Interface">
		<xsl:element name="Interface">
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir--></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="abstract"><xsl:value-of select="@isAbstract | GeneralizableElement.isAbstract/@xmi.value"/></xsl:attribute>
			<xsl:attribute name="final"><xsl:value-of select="@isLeaf"/></xsl:attribute>
			<!-- Attributs -->
			<xsl:apply-templates select="Classifier.feature/Attribute[@xmi.id]" mode="Property"/>
			<!-- Opérations -->
			<!--<xsl:apply-templates select="Classifier.feature/Operation[@xmi.id]" mode="Operation"/>-->
		</xsl:element>
	</xsl:template>
	<!-- Attributs -->
	<xsl:template match="Attribute" mode="Property">
		<xsl:element name="Property">
			<xsl:variable name="idType" select="StructuralFeature.type/Classifier/@xmi.idref"/>
			<xsl:variable name="type" select="//DataType[@xmi.id=$idType]/@name"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir--></xsl:attribute>
			<xsl:attribute name="type"><xsl:value-of select="$type"/></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="static"><xsl:value-of select="boolean(@ownerScope='classifier')"/></xsl:attribute>
			<xsl:attribute name="defaultValue"><xsl:value-of select="Attribute.initialValue/Expression/@body"/></xsl:attribute>
			<xsl:variable name="isEnum" select="boolean(//Stereotype[@name='enumeration']/Stereotype.extendedElement/ModelElement[@xmi.idref=$idType])"/>
			<xsl:if test="$isEnum">
				<xsl:attribute name="isEnum">true</xsl:attribute>
			</xsl:if>
		</xsl:element>
	</xsl:template>
	<!-- Operations -->
	<xsl:template match="Operation" mode="Operation">
		<xsl:element name="Operation">
			<xsl:variable name="returnTypeId" select="BehavioralFeature.parameter/Parameter/Parameter.kind[@xmi.value='return']/../Parameter.type/Classifier/@xmi.idref"/>
			<xsl:variable name="returnType" select="//DataType[@xmi.id=$returnTypeId]/@name"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="type"><xsl:value-of select="$returnType"/></xsl:attribute>
			<xsl:attribute name="visibility">v_<xsl:value-of select="@visibility"/></xsl:attribute>
			<xsl:attribute name="static"><xsl:value-of select="boolean(@ownerScope='classifier')"/></xsl:attribute>
			<xsl:attribute name="modifier">m_none</xsl:attribute>
			<xsl:attribute name="code"/>
			<xsl:for-each select="BehavioralFeature.parameter/Parameter/Parameter.kind[@xmi.value='in']">
				<xsl:element name="Parameter">
					<xsl:variable name="parameterTypeId" select="../Parameter.type/Classifier/@xmi.idref"/>
					<xsl:variable name="parameterType" select="//DataType[@xmi.id=$parameterTypeId]/@name"/>
					<xsl:attribute name="name"><xsl:value-of select="../@name"/></xsl:attribute>
					<xsl:attribute name="type"><xsl:value-of select="$parameterType"/></xsl:attribute>
					<xsl:attribute name="documentation"/>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:template>
	<!-- L'héritage -->
	<xsl:template match="Generalization" mode="Generalization">
		<xsl:element name="Generalization">
			<xsl:variable name="idClientClassName" select="Generalization.child/GeneralizableElement/@xmi.idref"/>
			<xsl:variable name="idSupplierClassName" select="Generalization.parent/GeneralizableElement/@xmi.idref"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="clientClassName"><xsl:value-of select="((//Class[@xmi.id=$idClientClassName]) | (//AssociationClass[@xmi.id=$idClientClassName]))/@name"/></xsl:attribute>
			<xsl:attribute name="supplierClassName"><xsl:value-of select="((//Class[@xmi.id=$idSupplierClassName]) | (//AssociationClass[@xmi.id=$idSupplierClassName]))/@name"/></xsl:attribute>
		</xsl:element>
	</xsl:template>
	<!-- L'implémentation -->
	<xsl:template match="Generalization" mode="Realization">
		<xsl:element name="Implementation">
			<xsl:variable name="idClientClassName" select="Generalization.child/GeneralizableElement/@xmi.idref"/>
			<xsl:variable name="idSupplierClassName" select="Generalization.parent/GeneralizableElement/@xmi.idref"/>
			<xsl:attribute name="name"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="clientClassName"><xsl:value-of select="((//Interface[@xmi.id=$idClientClassName]) | (//Class[@xmi.id=$idClientClassName]) | (//AssociationClass[@xmi.id=$idClientClassName]))/@name"/></xsl:attribute>
			<xsl:attribute name="supplierClassName"><xsl:value-of select="//Interface[@xmi.id=$idSupplierClassName]/@name"/></xsl:attribute>
		</xsl:element>
	</xsl:template>
	<!-- L'association -->
	<xsl:template match="Association" mode="Association">
		<xsl:element name="Association">
			<xsl:variable name="client">
				<xsl:apply-templates select="Association.connection" mode="GetAssociationClient"/>
			</xsl:variable>
			<xsl:variable name="supplier">
				<xsl:apply-templates select="Association.connection" mode="GetAssociationSupplier"/>
			</xsl:variable>
			<xsl:attribute name="name"><xsl:apply-templates select="Association.connection" mode="GetAssociationName"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="type"><xsl:apply-templates select="Association.connection" mode="GetAssociationType"/></xsl:attribute>
			<xsl:attribute name="navigable"><xsl:apply-templates select="Association.connection" mode="GetAssociationNavigability"/></xsl:attribute>
			<xsl:attribute name="associationClassifier"/>
			<xsl:attribute name="clientClassName"><xsl:apply-templates select="Association.connection" mode="GetAssociationClassName"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientRole"><xsl:apply-templates select="Association.connection" mode="GetAssociationRoleName"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientMultiplicity"><xsl:apply-templates select="Association.connection" mode="GetAssociationMultiplicity"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientQualifierName"/>
			<xsl:attribute name="clientQualifierType"/>
			<xsl:attribute name="supplierClassName"><xsl:apply-templates select="Association.connection" mode="GetAssociationClassName"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierRole"><xsl:apply-templates select="Association.connection" mode="GetAssociationRoleName"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierMultiplicity"><xsl:apply-templates select="Association.connection" mode="GetAssociationMultiplicity"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierQualifierName"/>
			<xsl:attribute name="supplierQualifierType"/>
		</xsl:element>
	</xsl:template>
	<!-- l'Association avec classe d'association-->
	<xsl:template match="AssociationClass" mode="Association">
		<xsl:element name="Association">
			<xsl:variable name="client">
				<xsl:apply-templates select="Association.connection" mode="GetAssociationClient"/>
			</xsl:variable>
			<xsl:variable name="supplier">
				<xsl:apply-templates select="Association.connection" mode="GetAssociationSupplier"/>
			</xsl:variable>
			<xsl:attribute name="name"><xsl:apply-templates select="Association.connection" mode="GetAssociationName"/></xsl:attribute>
			<xsl:attribute name="documentation"><!-- à voir --></xsl:attribute>
			<xsl:attribute name="type"><xsl:apply-templates select="Association.connection" mode="GetAssociationType"/></xsl:attribute>
			<xsl:attribute name="navigable"><xsl:apply-templates select="Association.connection" mode="GetAssociationNavigability"/></xsl:attribute>
			<xsl:attribute name="associationClassifier"><xsl:value-of select="@name"/></xsl:attribute>
			<xsl:attribute name="clientClassName"><xsl:apply-templates select="Association.connection" mode="GetAssociationClassName"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientRole"><xsl:apply-templates select="Association.connection" mode="GetAssociationRoleName"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientMultiplicity"><xsl:apply-templates select="Association.connection" mode="GetAssociationMultiplicity"><xsl:with-param name="role" select="$client"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="clientQualifierName"/>
			<xsl:attribute name="clientQualifierType"/>
			<xsl:attribute name="supplierClassName"><xsl:apply-templates select="Association.connection" mode="GetAssociationClassName"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierRole"><xsl:apply-templates select="Association.connection" mode="GetAssociationRoleName"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierMultiplicity"><xsl:apply-templates select="Association.connection" mode="GetAssociationMultiplicity"><xsl:with-param name="role" select="$supplier"/></xsl:apply-templates></xsl:attribute>
			<xsl:attribute name="supplierQualifierName"/>
			<xsl:attribute name="supplierQualifierType"/>
		</xsl:element>
	</xsl:template>
	<!-- retourne le nom de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationName">
		<xsl:choose>
			<xsl:when test="../@name != '' ">
				<xsl:value-of select="../@name"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="../@xmi.id"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- retourne le type de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationType">
		<xsl:variable name="class1" select="AssociationEnd[1]/AssociationEnd.aggregation/@xmi.value"/>
		<xsl:variable name="class2" select="AssociationEnd[2]/AssociationEnd.aggregation/@xmi.value"/>
		<xsl:choose>
			<xsl:when test="$class1='none' and $class2='none'">association</xsl:when>
			<xsl:when test="$class1='aggregate' or $class2='aggregate'">aggregation</xsl:when>
			<xsl:when test="$class1='composite' or $class2='composite'">composition</xsl:when>
		</xsl:choose>
	</xsl:template>
	<!-- retourne la navigabilité de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationNavigability">
		<xsl:variable name="client">
			<xsl:apply-templates select="Association.connection" mode="GetAssociationClient"/>
		</xsl:variable>
		<xsl:variable name="clientNavigability" select="AssociationEnd[position()=$client]/AssociationEnd.isNavigable/@xmi.value"/>
		<xsl:variable name="associationType">
			<xsl:apply-templates select="." mode="GetAssociationType"/>
		</xsl:variable>
		<xsl:choose>
			<xsl:when test="$associationType='association'">false</xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="$clientNavigability='true'">false</xsl:when>
					<xsl:otherwise>true</xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- retourne le client de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationClient">
		<xsl:variable name="associationType" select="AssociationEnd[1]/AssociationEnd.aggregation/@xmi.value"/>
		<xsl:choose>
			<xsl:when test="$associationType='none'">2</xsl:when>
			<xsl:otherwise>1</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- retourne le supplier de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationSupplier">
		<xsl:variable name="associationType" select="AssociationEnd[1]/AssociationEnd.aggregation/@xmi.value"/>
		<xsl:choose>
			<xsl:when test="$associationType='none'">1</xsl:when>
			<xsl:otherwise>2</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
	<!-- retourne le nom de la classe client/supplier de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationClassName">
		<xsl:param name="role"/>
		<xsl:variable name="clientId" select="AssociationEnd[position()=$role]/AssociationEnd.type/Classifier/@xmi.idref"/>
		<xsl:value-of select="(//Class[@xmi.id=$clientId] | //Interface[@xmi.id=$clientId] | //AssociationClass[@xmi.id=$clientId])/@name"/>
	</xsl:template>
	<!-- retourne le role du client/supplier de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationRoleName">
		<xsl:param name="role"/>
		<xsl:value-of select="AssociationEnd[position()=$role]/@name"/>
	</xsl:template>
	<!-- retourne la multiplicité du client/supplier de l'association -->
	<xsl:template match="Association.connection" mode="GetAssociationMultiplicity">
		<xsl:param name="role"/>
		<xsl:variable name="multiplicityLower" select="AssociationEnd[position()=$role]/AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.lower"/>
		<xsl:variable name="multiplicityUpper" select="AssociationEnd[position()=$role]/AssociationEnd.multiplicity/Multiplicity/Multiplicity.range/MultiplicityRange/MultiplicityRange.upper"/>

		<xsl:choose>
			<xsl:when test="$multiplicityLower='0' and $multiplicityUpper='1'" >0..1</xsl:when>
			<xsl:when test="$multiplicityLower='0' and $multiplicityUpper='-1'">0..*</xsl:when>
			<xsl:when test="$multiplicityLower='1' and $multiplicityUpper='1'">1</xsl:when>
			<xsl:otherwise>0..*</xsl:otherwise>
		</xsl:choose>

	</xsl:template>
</xsl:stylesheet>
